package com.lcq.demo.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.SendEmailRequest;
import software.amazon.awssdk.services.ses.model.SendEmailResponse;

import javax.annotation.Resource;

/**
 * @author 240701005
 */
@Service
@Slf4j
public class EmailService {

    @Resource
    private SesClient sesClient;

    public void sendEmail(String to, String subject, String body) throws Exception {
        SendEmailRequest request = SendEmailRequest.builder()
                .destination(b -> b.toAddresses(to))
                .message(m -> m
                        .body(b -> b.text(t -> t.data(body)))
                        .subject(s -> s.data(subject)))
                .source("hvyjps72196@chacuo.net")
                .build();
        try {
            SendEmailResponse response = sesClient.sendEmail(request);
            if (log.isInfoEnabled()) {
                log.info("sendEmail res: {}", response);
            }
            log.info("邮件发送成功~！Message ID:{}" , response.messageId());
        } catch (Exception e) {
            log.error("EmailService::sendEmail::error:{}", e.getMessage());
            throw new Exception("发送邮件失败!");
        }
    }

}
